<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="客户名称">
          <el-input
            v-model="searchParam.customerName"
            placeholder="请输入客户名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item label="项目名称">
          <el-input
            v-model="searchParam.projectName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="产品名称">
          <el-input
            v-model="searchParam.materialName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="报告日期">
          <el-date-picker
            v-model="searchParam.reportDateSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            @change="getreportDate"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        :header-cell-style="{ textAlign: 'center' }"
      >
        <el-table-column
          label="客户名称"
          prop="customerName"
          min-width="150"
          show-overflow-tooltip
          align="left"
        >
        </el-table-column>

        <el-table-column
          label="项目名称"
          prop="projectName"
          min-width="150"
          show-overflow-tooltip
          align="left"
        >
        </el-table-column>
        <el-table-column
          label="产品名称"
          prop="materialName"
          min-width="150"
          show-overflow-tooltip
          align="left"
        >
        </el-table-column>
        <el-table-column
          label="类型"
          prop="reportCategory"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("QmsReportType", scope.row.reportCategory) }}
          </template>
        </el-table-column>
        <el-table-column
          label="物料图号"
          prop="materialDrawingNo"
          min-width="150"
          show-overflow-tooltip
          align="left"
        >
        </el-table-column>
        <el-table-column
          label="物料批次号"
          prop="materialBatch"
          min-width="150"
          show-overflow-tooltip
          align="left"
        >
        </el-table-column>
        <el-table-column
          label="报告日期"
          prop="reportDate"
          min-width="180"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{
              scope.row.reportDate
                ? dayjs(scope.row.reportDate).format("YYYY-MM-DD")
                : ""
            }}
          </template>
        </el-table-column>

        <el-table-column
          label="审核状态"
          prop="approveStatus"
          min-width="150"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <!-- {{ useDictionary("ApproveStatus ", scope.row.approveStatus) }} -->
            <el-tag v-if="scope.row.approveStatus == 1" type="success">{{
              useDictionary("ApproveStatus", scope.row.approveStatus ?? "")
            }}</el-tag>
            <el-tag v-if="scope.row.approveStatus == 0">{{
              useDictionary("ApproveStatus", scope.row.approveStatus ?? "")
            }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200" align="center" fixed="right">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              查看
            </el-button>
            <el-button
              :disabled="scope.row.approveStatus != 0"
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              >编辑</el-button
            >
            <el-button
              :disabled="scope.row.approveStatus != 0"
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              >删除</el-button
            >
            <el-button
              :disabled="scope.row.approveStatus != 0"
              type="primary"
              size="small"
              link
              @click="examineItem(scope.row)"
              >审核</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <qualityReportRecordEdit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import dayjs from "dayjs";
defineOptions({
  name: "QMSQUALITYREPORTRECORD",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/qms/qmsreportrecord/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  customerId: "",
  customerName: "",
  materialId: "",
  materialCode: "",
  processId: "",
  projectId: "",
  reportDateSearch: [],
  reportDateStart: "",
  reportDateEnd: "",
};

const deleteParam = {
  api: "/qms/qmsreportrecord/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/qms/qmsreportrecord/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开质量报告记录表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改质量报告记录";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增质量报告记录";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开质量报告记录表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "质量报告记录详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getreportDate(data: any) {
  if (data) {
    searchParam.value.reportDateStart = data[0];
    searchParam.value.reportDateEnd = data[1];
  } else {
    searchParam.value.reportDateStart = "";
    searchParam.value.reportDateEnd = "";
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除该数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await request({
      url: `/qms/qmsreportrecord/deleteItem/${id}`,
      method: "post",
    }).then(() => {
      ElMessage.success("删除成功！");
      search();
    });
  });
}
// 审核
async function examineItem(row: any) {
  ElMessageBox.confirm("确认审核吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await request({
      url: `qms/qmsreportrecord/audit`,
      method: "post",
      data: {
        id: row.id,
      },
    }).then(() => {
      ElMessage.success("审核成功！");
      search();
    });
  });
}
</script>
